package wiki.hitime.stl.daq.service.admin;

import wiki.hitime.stl.daq.bean.db.DaqAcquisitionRecord;
import wiki.hitime.stl.daq.bean.db.DaqCheckRecord;
import wiki.hitime.stl.daq.bean.db.DaqMsgRecord;
import wiki.hitime.stl.daq.bean.dto.manage.CheckRecordDTO;
import wiki.hitime.stl.daq.bean.dto.manage.ExecuteRecordDTO;
import wiki.hitime.stl.daq.bean.dto.manage.MsgRecordDTO;
import wiki.hitime.stl.daq.bean.dto.req.ExecuteTaskRecordListReqDTO;
import wiki.hitime.stl.daq.enums.MsgTypeEnum;
import wiki.hitime.stl.daq.enums.ProcessStatusEnum;
import wiki.hitime.stl.daq.manager.DaqAcquisitionRecordManager;
import wiki.hitime.stl.daq.manager.DaqCheckRecordManager;
import wiki.hitime.stl.daq.manager.DaqExecuteRecordManager;
import wiki.hitime.stl.daq.manager.DaqMsgRecordManager;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhangdaoping
 * @create 2019-07-31 09:38
 */
@Service
public class RecordManageService {

    private DaqExecuteRecordManager daqExecuteRecordManager;
    private DaqAcquisitionRecordManager daqAcquisitionRecordManager;
    private DaqCheckRecordManager daqCheckRecordManager;
    private DaqMsgRecordManager daqMsgRecordManager;

    public RecordManageService(DaqExecuteRecordManager daqExecuteRecordManager,
                               DaqAcquisitionRecordManager daqAcquisitionRecordManager,
                               DaqCheckRecordManager daqCheckRecordManager,
                               DaqMsgRecordManager daqMsgRecordManager) {
        this.daqExecuteRecordManager = daqExecuteRecordManager;
        this.daqAcquisitionRecordManager = daqAcquisitionRecordManager;
        this.daqCheckRecordManager = daqCheckRecordManager;
        this.daqMsgRecordManager = daqMsgRecordManager;
    }

    /**
     * 分页获取记录
     *
     * @param reqDTO
     * @return
     */
    public PageInfo<ExecuteRecordDTO> listRecord(ExecuteTaskRecordListReqDTO reqDTO) {
        PageInfo<ExecuteRecordDTO> dtoPageInfo = daqExecuteRecordManager.listByPage(reqDTO);
        List<ExecuteRecordDTO> dtoList = dtoPageInfo.getList();
        List<Long> executeRecordIdList = dtoList.stream().map(ExecuteRecordDTO::getExecuteRecordId).collect(Collectors.toList());
        List<DaqAcquisitionRecord> acquisitionRecordList = daqAcquisitionRecordManager.list(executeRecordIdList);
        List<DaqCheckRecord> checkRecordList = daqCheckRecordManager.list(executeRecordIdList);
        List<DaqMsgRecord> msgRecordList = daqMsgRecordManager.list(executeRecordIdList);
        for (ExecuteRecordDTO record : dtoList) {
            Optional<DaqAcquisitionRecord> acquisitionResult = acquisitionRecordList.stream()
                    .filter(a -> a.getExecuteRecordId().equals(record.getExecuteRecordId()))
                    .findFirst();
            List<DaqCheckRecord> checkResultList = checkRecordList.stream()
                    .filter(a -> a.getExecuteRecordId().equals(record.getExecuteRecordId())).collect(Collectors.toList());
            List<DaqMsgRecord> msgResultList = msgRecordList.stream()
                    .filter(a -> a.getExecuteRecordId().equals(record.getExecuteRecordId())).collect(Collectors.toList());
            if (acquisitionResult.isPresent()) {
                ProcessStatusEnum statusEnum = ProcessStatusEnum.valueOf(acquisitionResult.get().getProcessStatus());
                Assert.notNull(statusEnum, "处理状态枚举为空");
                record.setAcquisitionProcessStatus(acquisitionResult.get().getProcessStatus());
                record.setAcquisitionProcessStatusName(statusEnum.getMsg());
                record.setAcquisitionProcessMsg(acquisitionResult.get().getProcessMsg());
            }
            List<CheckRecordDTO> checkRecordDTOList = new ArrayList<>();
            List<MsgRecordDTO> msgRecordDTOList = new ArrayList<>();
            checkResultList.forEach(r -> {
                CheckRecordDTO checkRecordDTO = new CheckRecordDTO();
                ProcessStatusEnum statusEnum = ProcessStatusEnum.valueOf(r.getProcessStatus());
                Assert.notNull(statusEnum, "处理状态枚举为空");
                checkRecordDTO.setCheckProcessStatus(r.getProcessStatus());
                checkRecordDTO.setCheckProcessStatusName(statusEnum.getMsg());
                checkRecordDTO.setCheckProcessMsg(r.getProcessMsg());
                checkRecordDTO.setCheckSummary(r.getCheckSummary());
                checkRecordDTOList.add(checkRecordDTO);
            });

            msgResultList.forEach(r -> {
                ProcessStatusEnum statusEnum = ProcessStatusEnum.valueOf(r.getProcessStatus());
                MsgTypeEnum msgTypeEnum = MsgTypeEnum.valueOf(r.getMsgType());
                Assert.notNull(statusEnum, "处理状态枚举为空");
                Assert.notNull(msgTypeEnum, "消息类型枚举为空");
                MsgRecordDTO msgRecordDTO = MsgRecordDTO.builder()
                        .msgProcessStatus(r.getProcessStatus())
                        .msgProcessStatusName(statusEnum.getMsg())
                        .msgProcessMsg(r.getProcessMsg())
                        .msgTypeName(msgTypeEnum.getMsg())
                        .msgContent(r.getMsgContent())
                        .build();
                msgRecordDTOList.add(msgRecordDTO);
            });

            record.setCheckRecordDTOList(checkRecordDTOList);
            record.setMsgRecordDTOList(msgRecordDTOList);
        }
        return dtoPageInfo;
    }
}
